import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_author/utils/statistics/event/backflow_event.dart';
import 'package:star_author/utils/statistics/statistics.dart';

import 'package:star_author/utils/utils.dart';
import 'package:star_author/widgets/button_widget.dart';

class BidderTipsDialog extends StatefulWidget {
  final String body;
  final String title;
  const BidderTipsDialog({
    Key? key,
    required this.body,
    required this.title,
  }) : super(key: key);

  @override
  State<BidderTipsDialog> createState() => _BidderTipsDialogState();
}

class _BidderTipsDialogState extends State<BidderTipsDialog> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    StatisticsUtil.event(SystemEvent.jing_biao_ti_shi_dian_ji, {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints(
        maxHeight: 580.w,
        minHeight: 100.w,
      ),
      child: Stack(
        children: [
          Positioned(
            child: Image.asset(
              'images/ic_bidder_tips_dialog_bg_lt.png',
              width: Get.width,
              fit: BoxFit.cover,
            ),
            top: 0,
            left: 0,
          ),
          Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: EdgeInsets.only(
                  top: 20.w,
                  bottom: 16.w,
                ),
                child: Text(
                  widget.title,
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: rgba(139, 87, 42, 1),
                    fontWeight: FontWeight.w500,
                  ),
                ),
                alignment: Alignment.center,
              ),
              buildContent(),
              Container(
                child: ButtonWidget(
                  child: Text(
                    "我知道了",
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: rgba(255, 255, 255, 1),
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  type: ButtonType.bgOrange,
                  width: Get.width - 24.w,
                  height: 40.w,
                  onPressed: () {
                    Get.back();
                  },
                ),
                margin: EdgeInsets.symmetric(
                  horizontal: 12.w,
                ),
              ),
              SizedBox(
                height: 20.w,
              ),
            ],
          ),
        ],
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(16.w),
          topLeft: Radius.circular(16.w),
        ),
        gradient: LinearGradient(
          colors: [
            rgba(248, 216, 106, 1),
            rgba(255, 255, 255, 1),
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
      ),
    );
  }

  TextStyle style = TextStyle(
    fontSize: 14.sp,
    color: rgba(102, 102, 102, 1),
  );

  buildContent() {
    Widget child = Container(
      alignment: Alignment.centerLeft,
      margin: EdgeInsets.only(
        bottom: 20.w,
        left: 12.w,
        right: 12.w,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: ListView(
        children: [
          Text(
            widget.body,
            style: style,
          ),
        ],
        shrinkWrap: true,
        padding: EdgeInsets.zero,
      ),
      padding: EdgeInsets.only(
        top: 13.w,
        left: 12.w,
        right: 12.w,
        bottom: 10.w,
      ),
    );
    TextPainter textPainter =
        getTextPainter(Get.context!, widget.body, style, Get.width - 24.w, 40);
    List<LineMetrics> list = textPainter.computeLineMetrics();
    double maxHeight = 580.w - 40.w - 20.w - 20.w - 20.w - 20.w - 20.w;
    double sum_height = 0;
    if (list.length > 0) {
      list.forEach((element) {
        sum_height += element.height;
      });
      if (sum_height > maxHeight) {
        return Expanded(child: child);
      }
    }
    return child;
  }
}
